package com.emm.yixun.website.controller.estate;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.ZNodes;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.estate.service.ProjectServiceFacade;
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.base.Const;
import com.emm.yixun.website.common.utils.security.ShiroUtil;

@Controller
@RequestMapping("/estate/treeComponent/")
public class TreeComponentController {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private ProjectServiceFacade projectServiceFacade;
	
	@RequestMapping(value="index", method=RequestMethod.GET)
	public  String index(Long projectId,ModelMap map,HttpServletRequest req){
		map.put("projectId", projectId);
		map.put("menuId", req.getParameter("menuId"));
		return "/estate/treeComponent/list";
	}
	
	@RequestMapping(value="findEstateTree", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes findEstateTree(String id,String other){
		if(StringUtils.isBlank(id))id="project_-1";
		if(StringUtils.isBlank(other))other="1";
		id=id.split("_")[1];
    	AjaxRes ar=new AjaxRes();
    	try {
			User user=ShiroUtil.getCurrentUser();
			ResponseList<ZNodes> responseList=projectServiceFacade.findEstateTree(user,Long.valueOf(id),other);
			if(responseList.isSuccess()){
	            if(null==responseList.getEntity())responseList.setEntity(new ArrayList<ZNodes>());     
				ar.setSucceed(responseList.getEntity());
			}else{
            	ar.setFailMsg(Const.DATA_FAIL);
            }
		} catch (Exception e) {
			logger.error(e.toString(),e);
			ar.setFailMsg(Const.DATA_FAIL);
		}
    	return ar;
    }
}
